function prt_tab(DATA, vnames, dig)
% -------------------------------------------------------------------------
% PURPOSE:  print DATA into LATEX table format 
% -------------------------------------------------------------------------
% USAGE:    prt_tab(DATA, vnames);
%
% INPUTS:   DATA (m by k) : m and k can by any integer
%                  vnames : variable names (set to [] if not determined)
%                     dig : number of digits
% -------------------------------------------------------------------------
% OUTPUTS:  
%           a LATEX table (need the 'threeparttable' package)
% -------------------------------------------------------------------------

% draft: 2015-01-01
% 1st update: 2020-08-09


    % 0. house cleean
    if nargin == 1
        vnames = [];
        dig = 4;
    elseif nargin == 2
        dig = 4;            
    end    

    % 1. analyze the DATA
    [m,k] = size(DATA);
    a = ['& %8.',num2str(dig),'f '];
    b = [repmat(a, 1, k) ' \\\\ ' '\n'];
    c = ['\\begin{tabular}{' repmat('c',1,k+1) '} \n'];
    
    % 2. print DATA in LATEX TABLE format
    fprintf('\\begin{table}[h!] \n')
    fprintf('\\centering \n')
    fprintf('\\caption{TITLE} \n')
    fprintf('\\scriptsize \n')
    fprintf('\\begin{threeparttable} \n')
    fprintf(c)
    fprintf('\\hline \n')
    for i = 1:m
        % 2.1 consider the case of no first column headers
        if isempty(vnames) == 1
            d = b;
        else
            vnames = strvcat(vnames);
            d = [vnames(i,:) b];
        end
        % 2.2 print the main body
        fprintf(d, DATA(i,:)')
    end
    % 2.3 print the rest of the table
    fprintf('\\hline \n')
    fprintf('\\end{tabular} \n')
    fprintf('\\begin{tablenotes} \n')
    fprintf('\\scriptsize \n')
    fprintf('\\item COMMENTS\n')
    fprintf('\\end{tablenotes} \n')
    fprintf('\\end{threeparttable} \n')
    fprintf('\\end{table} \n')
    fprintf('\n')
    fprintf('\n')
end

